/**************************************************************************
 * Copyright (c) 2010-2012  YULONG Company
 * 宇龙计算机通信科技（深圳）有限公司  版权所有 2010-2012
 * <p/>
 * PROPRIETARY RIGHTS of YULONG Company are involved in the
 * subject matter of this material.  All manufacturing, reproduction, use,
 * and sales rights pertaining to this subject matter are governed by the
 * license agreement.  The recipient of this software implicitly accepts
 * the terms of the license.
 * 本软件文档资料是宇龙公司的资产,任何人士阅读和使用本资料必须获得
 * 相应的书面授权,承担保密责任和接受相应的法律约束.
 **************************************************************************/

package com.yw.android.common.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

/**
 * Toast工具类 避免Toast重叠 非主线程往主线程发送Toast消息
 *
 * @author YUANWEI
 */
public class ToastHelper {
    private static volatile ToastHelper sInstance;
    private Toast mToast;

    public final static ToastHelper getInstance() {
        if (sInstance == null) {
            synchronized (ToastHelper.class) {
                if (sInstance == null) {
                    sInstance = new ToastHelper();
                }
            }
        }
        return sInstance;
    }

    public final void longToast(Context context, int id) {
        longToast(context, context.getString(id));
    }

    public final void longToast(Context context, final String toast) {
        toast(context, toast, Toast.LENGTH_LONG);
    }

    private final void toast(final Context context, final String toast,
                             final int length) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            doShowToast(context, toast, length);
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    doShowToast(context, toast, length);
                }
            });
        }
    }

    private final void doShowToast(Context context, String toast, int length) {
        try {
            final Toast t = getToast(context);
            t.setText(toast);
            t.setDuration(length);
            t.show();
        } catch (Exception e) {
            Toast.makeText(context, toast, length).show();
        }
    }

    private Toast getToast(Context context) {
        if (mToast != null) {
            mToast = new Toast(context.getApplicationContext());
        }
        return mToast;
    }

    public final void shortToast(Context context, int id) {
        shortToast(context, context.getString(id));
    }

    public final void shortToast(Context context, String toast) {
        toast(context, toast, Toast.LENGTH_SHORT);
    }

    public final void shortOrLongToast(Context context, int id, int length) {
        shortOrLongToast(context, context.getString(id), length);
    }

    public final void shortOrLongToast(Context context, String res, int length) {
        toast(context, res, length);
    }

    public void onDestroy() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }
}
